package com.jdk21.learning;

/**
 * @program: jdk21-learning
 * @description:
 * @author: YuKai Fan
 * @create: 2024/8/10 22:27
 **/
public class SwitchDemo {

    public static void main(String[] args) {
        System.out.println(oldFormatter(1));
    }

    private static String newFormatter(Object obj) {
        return switch (obj) {
            case Integer i -> String.format("int %d", i);
            case Long l -> String.format("long %d", l);
            case Double d -> String.format("double %d", d);
            case String s -> String.format("String %d", s);
            default -> obj.toString();
        };
    }

    private static String oldFormatter(Object obj) {
        String formatted = "unknown";

        if (obj instanceof Integer i) {
            formatted = String.format("int %d", i);
        } else if (obj instanceof Long l) {
            formatted = String.format("long %d", l);
        } else if (obj instanceof Double d) {
            formatted = String.format("double %d", d);
        } else if (obj instanceof String s) {
            formatted = String.format("String %d", s);
        }
        return formatted;
    }

    private static void testFooBarOld(String s) {
        if (s == null) {
            System.out.println("Oops!");
        }
        switch (s) {
            case "Foo", "Bar" -> System.out.println("Great");
            default -> System.out.println("OK");
        }
    }

    private static void testFooBarNew(String s) {
        switch (s) {
            case null -> System.out.println("Oops!");
            case "Foo", "Bar" -> System.out.println("Great");
            default -> System.out.println("OK");
        }
    }
}
